package com.changdu.seatunnel.admin.util;

import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

import com.changdu.seatunnel.admin.pojo.FieldMapping;
import com.changdu.seatunnel.admin.pojo.TableMapping;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class BaseTemplate {

	/**
	 * 返回默认值
	 * @param object
	 * @param defObject
	 * @return
	 */
	protected static String toString(Object object, String defObject) {
		if (Objects.nonNull(object)) return String.valueOf(object);
		
		return StringUtils.isNoneBlank(defObject) ? defObject : "";
	}
	
	/**
	 * 如果 源与目标映射字段完全一致，则不需要转换
	 * @param tableMapping
	 * @param jsonNode
	 * @return
	 */
	protected static JsonNode transformIgnore(TableMapping tableMapping, JsonNode template) {
		boolean mayBeIgnore = true;
		for (FieldMapping fieldMapping : tableMapping.getFieldMappings()) {
			if (!fieldMapping.getSourceField().equals(fieldMapping.getTargetField())) {
				mayBeIgnore = false;
				break;
			}
		}
		if (mayBeIgnore) {
			((ObjectNode) template).remove("transform");
		}
		return template;
	}
	
}
